/*
    Copyright © 2010  Ahti Legonkov
    
    This file is part of j+-.
    
    j+- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    j+- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package cx.ath.skyflyer.javapm.visitor;

import japa.parser.ast.body.BodyDeclaration;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.body.FieldDeclaration;
import japa.parser.ast.body.VariableDeclarator;
import japa.parser.ast.type.Type;
import japa.parser.ast.visitor.VoidVisitorAdapter;

/**
 * @author Ahti Legonkov
 */
public class FindFieldVisitor extends VoidVisitorAdapter {

    private FieldDeclaration field;
    private Type fieldType;
    private final String fieldName;
    
    /**
     *
     * @param fieldName name of field to find.
     */
    public FindFieldVisitor(String fieldName) {
        this.fieldName = fieldName;
    }

    public FieldDeclaration getField() {
        return field;
    }

    @Override
    public void visit(ClassOrInterfaceDeclaration n, Object arg) {
        for (BodyDeclaration member : n.getMembers()) {
            member.accept(this, arg);

            if (field != null)
                break;
        }
    }

    @Override
    public void visit(FieldDeclaration n, Object arg) {
        for (VariableDeclarator var : n.getVariables()) {
            if (var.getId().getName().equals(fieldName)) {
                field = n;
                return;
            }
        }
    }
}
